package com.letfox.ad.controller.ad;

import java.io.PrintWriter;
import java.util.List;

import net.sf.json.JSONArray;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.letfox.ad.controller.BaseController;
import com.letfox.ad.model.ADPlan;
import com.letfox.ad.model.ADServingDetail;
import com.letfox.ad.model.ADServingMaterial;
import com.letfox.ad.model.ADServingSetting;
import com.letfox.ad.model.ADUnitSchedule;
import com.letfox.ad.service.ADVertServingService;
import com.letfox.common.model.ResultResponse;

@Controller
@RequestMapping(value = "/advert")
public class ADVertServingController extends BaseController{
	
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private ADVertServingService advertServingService;
	
	@RequestMapping(value = "/adfound",method=RequestMethod.POST)
	@ResponseBody
	public ResultResponse advertFound(@ModelAttribute ADUnitSchedule adUnitSchedule){
		try {
			String account = request.getParameter("account");
			resultResponse=advertServingService.adFound(adUnitSchedule,account);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		
		return resultResponse;
	}
	
	@RequestMapping(value = "/adServing",method=RequestMethod.POST)
	@ResponseBody
	public ResultResponse adServing(@ModelAttribute ADServingSetting adServingSetting,@RequestParam String planId){		
		try {
			resultResponse=advertServingService.saveAdServing(adServingSetting, planId);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return resultResponse;
	}
	
	@RequestMapping(value = "/adServingMateral",method=RequestMethod.POST)
	@ResponseBody
	public ResultResponse adServingMateral(@ModelAttribute ADServingMaterial adServingMaterial,@RequestParam String planId){		
		try {
			resultResponse=advertServingService.saveADservingMaterlal(adServingMaterial, planId);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return resultResponse;
	}

	@RequestMapping(value = "/getUserAd",method=RequestMethod.POST)
	public void getUserAd(){
		String accountId = request.getParameter("account");
		List<ADPlan> lisPlans=null;
		try {		
			lisPlans=advertServingService.selectUserAD(accountId);
			JSONArray json = JSONArray.fromObject(lisPlans);
			PrintWriter out = response.getWriter();
			out.print(json);
			out.close();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}	
	}
	
	@RequestMapping(value = "/adPlanServing",method=RequestMethod.POST)
	@ResponseBody
	public ResultResponse adPlanServing(@ModelAttribute ADServingDetail adServingDetail,@RequestParam String planId){
		try {		
			resultResponse=advertServingService.saveAdDetal(adServingDetail, planId);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return resultResponse;
	}
	
}